package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

public class BannerAdapter extends VerticalViewPagerAdapter {

	private static final String TAG = "BannerAdapter";
    protected Context context;
    protected List<JSONObject> list;
	protected List<View> listView;
	protected ImageLoader imgLoader;
    
    public BannerAdapter(Context context, List<JSONObject> list) {
		this.list = list;
		this.context = context;
		listView = new ArrayList<View>();
		imgLoader = new ImageLoader(context, ContextUtils.getScreenWidth(context));
    }
    
    public boolean init() {
		if (null == list || list.isEmpty()) {
			return false;
		}
		for (int i = 0; i < list.size(); i ++) {
			JSONObject object = list.get(i);
			String id = null;
			String type = null;
			String adId = null;
			ViewHolder holder = null;
			holder = new ViewHolder();
			id = JSONUtils.getStringFromJSONObject(object,"adRefId");//图片ID
			type = JSONUtils.getStringFromJSONObject(object,"adType");//广告类型
			adId = JSONUtils.getStringFromJSONObject(object,"refId");//广告关键ID
			holder.setId(id);
			holder.setType(type);
			holder.setAdId(adId);
			if (null == id || null == type || null == adId || null == holder) {
				continue;
			}
			ImageView imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			imageView.setTag(holder);
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ImageView iv = (ImageView)v;
					ViewHolder vh = (ViewHolder)iv.getTag();
					String adType = vh.getType();
					Intent intent = null;
					if ("0".equals(adType)) {//产品类型
						intent = new Intent(context, ProductItemActivity.class);
					} else if ("1".equals(adType)) {//求购类型
						intent = new Intent(context, PurchaseItemActivity.class);
					} else if ("2".equals(adType)) {//公司类型
						intent = new Intent(context, CompanyItemActivity.class);
					} else {
						Toast.makeText(context, "未知的广告类型", Toast.LENGTH_SHORT).show();
						return;
					}
					intent.putExtra("id", vh.getAdId());
					context.startActivity(intent);
				}
			});
			imgLoader.displayImage(Constants.APP_URL + "/assistant/image.html?id=" + id, imageView);
			listView.add(imageView);
		}
		return true;
	}
    
	@Override
	public int getCount() {
		return listView.size();
	}
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((VerticalViewPager) arg0).removeView(listView.get(arg1));
	}
	@Override
	public Object instantiateItem(View arg0, int arg1) {
		((VerticalViewPager) arg0).addView(listView.get(arg1));
		return listView.get(arg1);
	}
	protected class ViewHolder {
		protected String id = null;
		protected String type = null;
		protected String adId = null;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public String getAdId() {
			return adId;
		}
		public void setAdId(String adId) {
			this.adId = adId;
		}
	}
}
